package com.easy.dfc.service;

import com.easy.common.bean.Result;
import com.easy.common.constant.EnumConfig;
import com.easy.dfc.Utils.SensitivewordEngine;
import com.easy.dfc.Utils.Util;
import com.easy.dfc.entity.*;
import com.easy.dfc.mapper.AnswerQuestionMapper;
import com.easy.dfc.mapper.AskOtherMapper;
import com.easy.dfc.mapper.ReplyAnswerMapper;
import com.easy.dfc.mapper.SensitiveWordsMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class AskOtherService {

    @Resource
    private AskOtherMapper askOtherMapper;

    @Resource
    private AnswerQuestionMapper answerQuestionDao;

    @Resource
    private SensitiveWordsMapper sensitiveWordsMapper;

    @Resource
    private ReplyAnswerMapper replyAnswerMapper;

    public Result addQuestion(ParaAskother para, String path) {
        Result rt = new Result();
        AskOther askOther = new AskOther();
        askOther.setUserId(Util.ToUuidString(para.getUserId().replaceAll("-", "")));

        askOther.setTitle(para.getTitle());
        //进行敏感词处理
        askOther.setQuestion(SensitivewordEngine.DealWithSensitiveword(sensitiveWordsMapper.selectAll(), Util.getRequestString(para.getQuestion())));
        askOther.setCreateBy(new Date());
        askOther.setProductUuid(para.getProductUuid());
        //图片上传到阿里云
        if (path != null) {
            askOther.setImage(path);
        }
        else {
            askOther.setImage(para.getImages());
        }

        if(askOtherMapper.addQuestion(askOther)>0){
            rt.setStatus(EnumConfig.S_CODE.OK);
        }
        else {
            rt.setStatus(EnumConfig.S_CODE.FAIL);
        }
        return rt;
    }

    public Result getQuestionList(ParaAskother para) {
        Result rt = new Result();
        List<AskOther> list = askOtherMapper.getQuestionList(para);
        rt.setStatus(EnumConfig.S_CODE.OK);
        rt.setData(list);
        return rt;
    }

    public Result addAnswer(ParaAskother para,String path) {
        Result rt = new Result();
        AnswerQuestion answerQuestion = new AnswerQuestion();
        answerQuestion.setUserId(Util.ToUuidString(para.getUserId().replaceAll("-", "")));
        //进行敏感词处理
        answerQuestion.setAnswer(SensitivewordEngine.DealWithSensitiveword(sensitiveWordsMapper.selectAll(), Util.getRequestString(para.getAnswer())));
        answerQuestion.setCreateBy(new Date());
        answerQuestion.setQuestionId(para.getQuestionId());
        //图片上传到阿里云
        if (path != null) {
            answerQuestion.setImage(path);
        }
        else {
            answerQuestion.setImage(para.getImages());
        }

        if(answerQuestionDao.addAnswer(answerQuestion)>0){
            rt.setStatus(EnumConfig.S_CODE.OK);
        }
        else {
            rt.setStatus(EnumConfig.S_CODE.FAIL);
        }
        return rt;

    }

    public Result selectAnswerList(ParaAskother para) {

        Result rt = new Result();
        List<AnswerQuestion> list = answerQuestionDao.selectAnswerList(para);
        rt.setStatus(EnumConfig.S_CODE.OK);
        rt.setData(list);
        return rt;
    }

    public Result addReply(ParaAskother para) {
        Result rt = new Result();
        ReplyAnswer replyAnswer = new ReplyAnswer();
        replyAnswer.setUserId(Util.ToUuidString(para.getUserId().replaceAll("-", "")));
        //敏感词处理
        replyAnswer.setReply(SensitivewordEngine.DealWithSensitiveword(sensitiveWordsMapper.selectAll(), Util.getRequestString(para.getReply())));
        replyAnswer.setAnswerId(para.getAnswerId());
        replyAnswer.setCreateBy(new Date());
        if(replyAnswerMapper.addReply(replyAnswer)>0){
            rt.setStatus(EnumConfig.S_CODE.OK);
        }
        else {
            rt.setStatus(EnumConfig.S_CODE.FAIL);
        }

        return rt;
    }

    public Result selectReplyList(ParaAskother para) {
        Result rt = new Result();
        List<ReplyAnswer> list = replyAnswerMapper.selectReplyList(para);
        rt.setStatus(EnumConfig.S_CODE.OK);
        rt.setData(list);
        return rt;
    }

    public Result getQuestionAll(ParaAskother para) {
        Result rt = new Result();
        List<AskOther> list = askOtherMapper.getQuestionList(para);
        List li = new ArrayList();
        for(int i=0;i <list.size();i++){
            AskOther ask = list.get(i);
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("id",ask.getId());
            map.put("title",ask.getTitle());
            map.put("question",ask.getQuestion());
            map.put("createBy",ask.getCreateBy());
            map.put("answerNum",ask.getNum());
            map.put("avatar",ask.getAvatar());
            map.put("nick",ask.getNick());
            para.setQuestionId(ask.getId());
            para.setPageIndex(0);
            para.setPageSize(3);
            List<AnswerQuestion> answerList = answerQuestionDao.selectAnswerList(para);
            map.put("answerList",answerList);
            li.add(map);
        }
        rt.setStatus(EnumConfig.S_CODE.OK);
        rt.setData(li);
        return rt;
    }

    public Result getQuestionDetail(ParaAskother para) {
        Result rt = new Result();
        AskOther ask = askOtherMapper.getQuestionById(para.getQuestionId());
        rt.setStatus(EnumConfig.S_CODE.OK);
        rt.setData(ask);
        return rt;

    }

    public Result getMyQuestionList(ParaAskother para) {
        Result rt = new Result();
        List<AskOther> list = askOtherMapper.getMyQuestionList(para);
        List li = new ArrayList();
        for(int i=0;i <list.size();i++){
            AskOther ask = list.get(i);
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("id",ask.getId());
            map.put("title",ask.getTitle());
            map.put("question",ask.getQuestion());
            map.put("createBy",ask.getCreateBy());
            map.put("answerNum",ask.getNum());
            map.put("avatar",ask.getAvatar());
            map.put("nick",ask.getNick());
            para.setQuestionId(ask.getId());
            para.setPageIndex(0);
            para.setPageSize(3);
            List<AnswerQuestion> answerList = answerQuestionDao.selectAnswerList(para);
            map.put("answerList",answerList);
            li.add(map);
        }
        rt.setStatus(EnumConfig.S_CODE.OK);
        rt.setData(li);
        return rt;
    }


    public Result getMyAnswerQuestionList(ParaAskother para) {
        Result rt = new Result();
        List<AskOther> list = askOtherMapper.getMyAnswerQuestionList(para);
        List li = new ArrayList();
        for(int i=0;i <list.size();i++){
            AskOther ask = list.get(i);
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("id",ask.getId());
            map.put("title",ask.getTitle());
            map.put("question",ask.getQuestion());
            map.put("createBy",ask.getCreateBy());
            map.put("answerNum",ask.getNum());
            map.put("avatar",ask.getAvatar());
            map.put("nick",ask.getNick());
            para.setQuestionId(ask.getId());
            para.setPageIndex(0);
            para.setPageSize(3);
            List<AnswerQuestion> answerList = answerQuestionDao.selectAnswerList(para);
            map.put("answerList",answerList);
            li.add(map);
        }
        rt.setStatus(EnumConfig.S_CODE.OK);
        rt.setData(li);
        return rt;
    }
}
